﻿using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui._13.LAIHANGL
{
    public partial class YUEDUTZSL : BasePage
    {
        #region 属性

        #endregion

        #region 变量
        YS_LH_LAIHANXXProcess blllh = new YS_LH_LAIHANXXProcess();

        YS_LH_YONGHUXXProcess laihanyhxx = new YS_LH_YONGHUXXProcess();

        /// <summary>
        /// 访问数据库
        /// </summary>
        SpHelper _sphelper = new SpHelper();

        /// <summary>
        /// 月调整
        /// </summary>
        YS_LH_YUETIAOZProcess bllyue = new YS_LH_YUETIAOZProcess();

        /// <summary>
        /// 流程
        /// </summary>
        YS_LH_LAIHANLCJLBLL lcbll = new YS_LH_LAIHANLCJLBLL();

        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        protected void BtnBack_Click(object sender, EventArgs e)
        {

            Save(-2);
        }
        protected void BtnSL_Click(object sender, EventArgs e)
        {

            Save(2);
        }
        protected void BtnBSL_Click(object sender, EventArgs e)
        {
            Save(-1);
        }
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            txtLiYou.Focus();
        }
        #endregion

        #region 方法
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="type">0退回 2受理 -1不受理</param>
        private void Save(int type)
        {
            if (txtLiYou.Text.Trim().ToString().Length == 0)
            {
               
                PageTools.AlertMsg(this, "申请理由不能为空");
                return;
            }
            string updatecol = "";
            updatecol = " S_SHENQINGLY='{0}',S_BEIZHU='{1}',I_SHURUR={2},D_SHURURQ=sysdate ";
            updatecol = string.Format(updatecol, txtLiYou.Value, txtBeiZhu.Value,LoginInfo.CurrentUser.UserID);
            string wherecase = string.Format(" ID={0}", h_yue.Value);

            string laihanzbupdatecol = string.Format("I_LIUCHENGBH={0},S_SHENQINGLY='{1}',S_BEIZHU='{2}'", 3,txtLiYou.Text,txtBeiZhu.Text);
            if (type == -1)
            {
                laihanzbupdatecol = string.Format("S_SHENQINGLY='{1}',S_BEIZHU='{2}' ,S_LIUCHENGZT=", type, txtLiYou.Text, txtBeiZhu.Text);
            
            }
            else if (type == -2)
            {
                laihanzbupdatecol = string.Format("I_LIUCHENGBH={0},S_SHENQINGLY='{1}',S_BEIZHU='{2}'", 0, txtLiYou.Text, txtBeiZhu.Text);
            
            }
            string where = string.Format(" ID={0} ",laihanid.Value );
            int success = bllyue.UpdateYS_LH_YUETIAOZsDynamic(updatecol, wherecase);
            success += blllh.UpdateYS_LH_LAIHANXXesDynamic(laihanzbupdatecol,where);
            if (success > 0)
            {
                ///添加受理开始信息
                YS_LH_LAIHANLCJL lcjl = new YS_LH_LAIHANLCJL();
                lcjl.D_CAOZUORQ = DateTime.Now;
                lcjl.D_STARTDATE = DateTime.Now;
                lcjl.I_CAOZUOR = LoginInfo.CurrentUser.UserID;
                lcjl.I_LAIHANLX = 1;
                lcjl.I_LIUCHENGBH = type;
                
                lcjl.ID = 0;

                if (type == -2||type==-1)
                {
                    lcjl.D_ENDDATE = DateTime.Now;
                }

                DataTable lcxx = lcbll.SelectYS_LH_LAIHANLCJLsDynamic(string.Format(" S_LAIHANBH='{0}' and I_LIUCHENGBH={1} ", UCLHUserInfo.LAIHANBH, type), "", null);
                if (lcxx != null && lcxx.Rows.Count > 0)
                {
                    lcjl.ID = Convert.ToInt32(lcxx.Rows[0]["ID"]);
                }
                lcjl.S_LAIHANBH = UCLHUserInfo.LAIHANBH;

                lcjl.S_LIUCHENGMC = type == 2 ? "受理" : type==-1?"不受理":"退回";

                _SYS_LOGBLL.Log<YS_LH_LAIHANLCJL>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "来函月登记受理-" + lcjl.S_LIUCHENGMC + "(" + (Convert.ToInt32(txtJHL.Text) * 100) + ")", OperateState.Modify, lcjl, null);
                lcbll.InsertOrUpdate(lcjl);

                


                ///更新受理结束事件
                lcbll.UpdateYS_LH_LAIHANLCJLsDynamic(string.Format("D_ENDDATE=sysdate,I_CAOZUOR={0},D_CAOZUORQ=sysdate", LoginInfo.CurrentUser.UserID), " I_LIUCHENGBH=2 and D_ENDDATE is null and S_LAIHANBH='" + lcjl.S_LAIHANBH + "'");
                if (type > 0)
                {
                    AddYEWUYUAN();
                    lcbll.UpdateYS_LH_LAIHANLCJLsDynamic(string.Format("D_ENDDATE=sysdate,I_CAOZUOR={0},D_CAOZUORQ=sysdate", LoginInfo.CurrentUser.UserID), " I_LIUCHENGBH=1 and S_LAIHANBH='" + lcjl.S_LAIHANBH + "'");
                
                }


                PageTools.AlertMsg(this, "操作完成");

                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), DateTime.Now.ToString("yyyyMMddHHmmssfff"), new System.Text.StringBuilder("window.close();").ToString(), true);
        
            }

        }
        private void AddYEWUYUAN()
        {
            ///添加流程记录业务员意见开始信息
            YS_LH_LAIHANLCJL lcjl = new YS_LH_LAIHANLCJL();
            lcjl.D_CAOZUORQ = DateTime.Now;
            lcjl.D_STARTDATE = DateTime.Now;
            lcjl.I_CAOZUOR = LoginInfo.CurrentUser.UserID;
            lcjl.I_LAIHANLX = 1;
            lcjl.I_LIUCHENGBH = 3;
            lcjl.ID = 0;

            DataTable lcxx = lcbll.SelectYS_LH_LAIHANLCJLsDynamic(string.Format(" S_LAIHANBH='{0}' and I_LIUCHENGBH={1} ", UCLHUserInfo.LAIHANBH, 3), "", null);
            if (lcxx != null && lcxx.Rows.Count > 0)
            {
                lcjl.ID = Convert.ToInt32(lcxx.Rows[0]["ID"]);
            }
            lcjl.S_LAIHANBH = UCLHUserInfo.LAIHANBH;

            lcjl.S_LIUCHENGMC ="业务员意见";

            lcbll.InsertOrUpdate(lcjl);
           
        }
        private void BindData()
        {
            if (string.IsNullOrEmpty(Request["id"])||Request["id"]=="null")
            {
                return;
            }
            decimal id = Convert.ToDecimal(Request["id"]);

            YS_LH_LAIHANXX lh = blllh.SelectYS_LH_LAIHANXX(id);
            if (lh == null)
            {
                return;
            }
            UCLHUserInfo.YONGHUH = lh.S_YONGHUBH;
            UCLHUserInfo.YONGHUHINPUT = lh.S_YONGHUBH;
            UCLHUserInfo.XUKEBH = lh.S_XUKEBH;
            UCLHUserInfo.TONGYIBM = lh.S_TONGYIBM;
            UCLHUserInfo.LAIHANBH = lh.S_LAIHANBH;
            UCLHUserInfo.GUANLIKBH = lh.S_GUANLIKBH;
            UCLHUserInfo.ZHANDIANBH = lh.S_ZHANDIANBH;
            HidListName.Set("laihanlx",lh.I_LAIHANLX);

            laihanid.Value = lh.ID.ToString();

            h_laihanbh.Value = lh.S_LAIHANBH;
            h_yonghuh.Value = lh.S_YONGHUBH;

            DataTable userdt = laihanyhxx.SelectYS_LH_YONGHUXXesDynamic(string.Format(" S_YONGHUBH='{0}' and S_LAIHANBH='{1}'", lh.S_YONGHUBH, lh.S_LAIHANBH), "", null);
            if (userdt != null && userdt.Rows.Count > 0)
            {
                UCLHUserInfo.YONGHUM = userdt.Rows[0]["S_YONGHUM"].ToString();
                UCLHUserInfo.DIZHI = userdt.Rows[0]["S_DIZHI"].ToString();

                UCLHUserInfo.LIANXIBM = userdt.Rows[0]["S_LIANXIBM"].ToString();

                UCLHUserInfo.YOUBIAN = userdt.Rows[0]["S_YOUBIAN"].ToString();
                UCLHUserInfo.LIANXIR = userdt.Rows[0]["S_LIANXIR"].ToString();
                UCLHUserInfo.LIANXIDH = userdt.Rows[0]["S_LIANXIDH"].ToString();
            }

            DataTable yuetz = bllyue.SelectYS_LH_YUETIAOZsDynamic(string.Format("S_LAIHANBH='{0}'", lh.S_LAIHANBH), "  ID desc", null);
            if (yuetz != null && yuetz.Rows.Count > 0)
            {

                UCLHUserInfo.Y = yuetz.Rows[0]["I_Y"].ToString();
                UCLHUserInfo.M = yuetz.Rows[0]["I_M"].ToString();
                txtJHL.Text = (Convert.ToInt32(yuetz.Rows[0]["I_SHENQINGL"].ToString()) / 100).ToString();
                chkBAI.Checked = true;
                h_yue.Value = yuetz.Rows[0]["ID"].ToString();
                int nowM = 1;
                int nowY=0;

                if (Convert.ToInt32(UCLHUserInfo.M) == 1)
                {
                    nowM = 12;
                    nowY = Convert.ToInt32(UCLHUserInfo.Y) - 1;
                }
                else
                {
                    nowM = Convert.ToInt32(UCLHUserInfo.M)-1;
                    nowY = Convert.ToInt32(UCLHUserInfo.Y);
                }
                /*
                int? syl_sy, yjh;
                DataSet ds = new SpHelper().P_YS_LH_YONGHUYSSJCX((int?)lh.I_TZY, (int?)lh.I_TZM, lh.S_YONGHUBH, out yjh, out syl_sy);
                this.txtJHL.Text = yjh.ToString();
                this.txtSYL.Text = syl_sy.ToString();
                */  
                this.txtJHL.Text =Convert.ToString(yuetz.Rows[0]["I_JH_Y"]) ;
                this.txtSYL.Text = Convert.ToString(yuetz.Rows[0]["I_SYL_SY"]);
                /*
                DataSet ds = _sphelper.P_YS_LH_YONGHUYSSJCX(nowY, nowM, lh.S_YONGHUBH.Trim(), null);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    try
                    {
                        int YJH = Convert.ToInt32(ds.Tables[0].Rows[0]["I_YUANJIHUA"]);
                        int SYL = Convert.ToInt32(ds.Tables[0].Rows[0]["I_SHIYONGSL"]);
                        txtYJH.Text = (YJH).ToString();
                        txtSYL.Text = (SYL).ToString();

                        chkBAI.Checked = true;
                    }
                    catch (Exception ex)
                    {
                        PageTools.AlertMsg(this, ex.Message);
                    }

                }
                  */
            }
            
            
        }
        #endregion
    }
}